package gov.nist.ssd.swqb.commands;

import com.hp.hpl.jena.rdf.model.RDFNode;
import gov.nist.ssd.swqb.core.Display;
import gov.nist.ssd.swqb.core.exceptions.InvalidURIException;
import gov.nist.ssd.swqb.core.exceptions.OutOfBoundsException;
import gov.nist.ssd.swqb.core.exceptions.TimeoutException;
import gov.nist.ssd.swqb.gui.ErrorDisplayer;

/**
 *
 * @author Benjamin Herbomez
 */
public class SetFocus implements Command {

    private Display display;
    private RDFNode nextNode;
    private RDFNode currentNode;
    private int currentPage;

    public SetFocus(Display display, RDFNode nextNode) {
        this.display = display;
        this.nextNode = nextNode;
        this.currentNode = display.getCurrentFocus();
        this.currentPage = display.getCurrentPageIndex();
    }

    @Override
    public void exec() {
        try {
            display.setFocus(this.nextNode);
        } catch (TimeoutException ex) {
            ErrorDisplayer.getInstance().displayError(ex);
        } catch (InvalidURIException ex) {
            ErrorDisplayer.getInstance().displayError("Invalide URI");
        }
    }

    @Override
    public void unExec() {
        try {
            this.display.setFocus(currentNode);
            this.display.setPageIndex(currentPage);
        } catch (OutOfBoundsException ex) {
            ErrorDisplayer.getInstance().displayError("Out of bound");
        } catch (TimeoutException ex) {
            ErrorDisplayer.getInstance().displayError(ex);
        } catch (InvalidURIException ex) {
            ErrorDisplayer.getInstance().displayError("Invalide URI");
        }
    }
}
